﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave
{
    public class TimeSpanHelper
    {
        private static string andText = string.Format(" {0} ", Properties.Resources.And);

        public static string ToString(TimeSpan t, bool includeMajorZeros, bool includeMinorZeros)
        {
            string r = "";
            bool isInMajor = true;

            //Days
            AddToString(ref r, ref isInMajor, includeMajorZeros, includeMinorZeros, t.Days, Properties.Resources.TimeSpanHelper_Day);
            //Hours
            AddToString(ref r, ref isInMajor, includeMajorZeros, includeMinorZeros, t.Hours, Properties.Resources.TimeSpanHelper_Hour);
            //Minutes
            AddToString(ref r, ref isInMajor, includeMajorZeros, includeMinorZeros, t.Minutes, Properties.Resources.TimeSpanHelper_Minute);
            //Seconds
            AddToString(ref r, ref isInMajor, includeMajorZeros, includeMinorZeros, t.Seconds, Properties.Resources.TimeSpanHelper_Second);

            return r;
        }
        private static void AddToString(ref string r, ref bool isInMajor, bool includeMajorZeros, bool includeMinorZeros, int valueNum, string valueFormat)
        {
            if (valueNum > 0)
                isInMajor = false;
            if (((isInMajor) ? includeMajorZeros : includeMinorZeros) || valueNum > 0)
                r += (string.IsNullOrEmpty(r) ? "" : andText) + string.Format(valueFormat, valueNum);
        }

        public static TimeSpan RoundInMinutes(TimeSpan toRound, int number)
        {
            return RoundInMinutes(toRound, number, true);
        }
        public static TimeSpan RoundInMinutes(TimeSpan toRound, int number, bool rountToUp)
        {
            int total = (int)toRound.TotalMinutes;
            int remainder = total % number;
            TimeSpan r = new TimeSpan(0, total - remainder + ((remainder == 0) ? 0 : ((rountToUp) ? number : 0)), 0);
            return r;
        }
    }
}
